<template>


  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-row>
      <el-col :span="12">
        <div style="display: inline">
          <el-carousel height="600px">
            <el-carousel-item v-for="item in fileList" :key="item.fileId">

<!--                <el-image-->
<!--                  style="width: 400px; height: 400px;object-fit: contain"-->
<!--                  class="hello"-->
<!--                  :fit="dataForm.fit"-->
<!--                  src="https://i.loli.net/2021/07/04/mVthNaqOsocLj2y.png"></el-image>-->
              <img :src="item.url" :alt="item.url" style="height: 400px;width: 300px;object-fit: contain">


            </el-carousel-item>
          </el-carousel>
        </div>
      </el-col>
      <el-col :span="12">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
          <el-form-item label="书名" prop="name">
            <el-input v-model="dataForm.name" placeholder="书名" readonly></el-input>
          </el-form-item>
<!--          <el-form-item label="书名" prop="image">-->
<!--            <div style="display: block">-->
<!--              <el-carousel height="150px">-->
<!--                <el-carousel-item v-for="item in dataForm.image" :key="item">-->

<!--                  &lt;!&ndash;                    <el-image&ndash;&gt;-->
<!--                  &lt;!&ndash;                      style="width: 400px; height: 400px"&ndash;&gt;-->
<!--                  &lt;!&ndash;                      class="hello"&ndash;&gt;-->
<!--                  &lt;!&ndash;                      :fit="dataForm.fit"&ndash;&gt;-->
<!--                  &lt;!&ndash;                      src="https://i.loli.net/2021/07/04/mVthNaqOsocLj2y.png"></el-image>&ndash;&gt;-->
<!--                  <img src="https://i.loli.net/2021/07/04/mVthNaqOsocLj2y.png" style="height: 400px;width: 500px">-->
<!--                  <img src="https://i.loli.net/2021/07/04/mVthNaqOsocLj2y.png" style="height: 400px;width: 500px">-->

<!--                </el-carousel-item>-->
<!--              </el-carousel>-->
<!--            </div>-->

<!--          </el-form-item>-->

          <el-form-item label="版本" prop="version">
            <el-input v-model="dataForm.version" placeholder="版本" readonly></el-input>
          </el-form-item>
          <el-form-item label="出版商" prop="vendor">
            <el-input v-model="dataForm.vendor" placeholder="出版商" readonly></el-input>
          </el-form-item>
          <el-form-item label="作者" prop="vendor">
            <el-input v-model="dataForm.authorNames" placeholder="作者" readonly></el-input>
          </el-form-item>

          <el-form-item label="出版时间" prop="publicDate">
            <el-date-picker
              type="date"
              :clearable="false"
              v-model="dataForm.publicDate"
              value-format="yyyy-MM-dd 00:00:00"
              placeholder="出版时间" readonly></el-date-picker>
          </el-form-item>
          <el-form-item label="简介" prop="summary">
            <el-input v-model="dataForm.summary"
                      placeholder="简介"
                      type="textarea"
                      :autosize="{ minRows: 3, maxRows: 10}" readonly></el-input>
          </el-form-item>

          <el-form-item label="状态" size="mini" prop="status">
            <el-radio-group v-model="dataForm.status">
              <el-radio :label="0" disabled>禁用</el-radio>
              <el-radio :label="1" disabled>正常</el-radio>
            </el-radio-group>
          </el-form-item>

          <el-form-item label="当前库存" prop="total">
            <el-input-number v-model="dataForm.available" disabled placeholder="图书数量"></el-input-number>
          </el-form-item>

          <el-form-item label="借阅数量" prop="borrowCount">
            <el-input-number v-model="dataForm.borrowCount" :max="dataForm.available" :min="0"
                             placeholder="图书数量"></el-input-number>
          </el-form-item>

          <el-form-item label="还书时间" prop="limitTime">
            <el-date-picker
              type="date"
              :clearable="false"
              v-model="dataForm.limitTime"
              value-format="yyyy-MM-dd 00:00:00"
              placeholder="还书时间"></el-date-picker>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>




    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" v-prevent-db-click @click="dataFormSubmit()">借阅</el-button>
    </span>
  </el-dialog>

</template>

<script>
import '@/utils/preventDbClick'

export default {
  data () {
    var filterByName = (query, item) => {
      return item.label.indexOf(query) > -1
    }
    let validateNumber = (rule, value, callback) => {
      console.log(`value is ${value}`)
      if (parseInt(value) === 0) {
        callback(new Error('借阅数量为空'))
      } else {
        callback()
      }
    }

    return {
      visible: false,
      filterByName,
      authorList: [],
      authorMap: {},
      fileList: [],
      dataForm: {
        id: 0,
        name: '',
        version: '',
        vendor: '',
        summary: '',
        status: 1,
        publicDate: '',
        total: '',
        available: 0,
        authorIdList: [],
        createTime: '',
        authorNames: '',
        borrowCount: 0,
        limitTime: '',
        image: [
          'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg', 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg', 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg'],
        fit: 'contain'
      },

      dataRule: {
        borrowCount: [
          // { required: true, message: '借阅数量为空', trigger: 'blur' },
          {validator: validateNumber, message: '借阅数量为空', trigger: 'blur'}
        ],
        limitTime: [
          {required: true, message: '还书时间不能为空', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0

      this.$http({
        url: this.$http.adornUrl('/sys/author/select'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({data}) => {
        this.authorList = data && data.code === 0 ? data.list.map((e) => {
          return {key: e.authorId, label: e.username}
        }) : []
        data.list.forEach((item) => {
          this.authorMap[item['authorId']] = item
        })
        console.log(this.authorList)
      }).then(() => {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      }).then(() => {
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sys/book/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.book.name
              this.dataForm.version = data.book.version
              this.dataForm.vendor = data.book.vendor
              this.dataForm.summary = data.book.summary
              this.dataForm.status = data.book.status
              this.dataForm.publicDate = data.book.publicDate
              this.dataForm.total = data.book.total
              this.dataForm.available = data.book.available
              this.dataForm.authorIdList = data.book.authorIdList
              this.dataForm.createTime = data.book.createTime
              this.fileList = data.book.fileList.map(e => {
                return {name: e.filename, url: e.url, fileId: e.fileId}
              })
              this.dataForm.authorNames = data.book.authorIdList.map((e) => {
                return this.authorMap[e]['username']
              }).join(',')
            }
          })
        }
      })
    },

    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sys/borrow/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'total': this.dataForm.borrowCount,
              'limitTime': this.dataForm.limitTime,
              'bookId': this.dataForm.id || undefined
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
